﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HtmlAgilityPack; // Open source pack.
using System.Net;
using GraphControl;
using System.Threading;



namespace TeamPirateForm
{
    public partial class Form1 : Form
    {

        public GraphControl.BasicGraph graph;
        public Form1()
        {
            InitializeComponent();
            graph = new BasicGraph();
        }

        private void btn_Crawl_Click(object sender, EventArgs e)
        {
            ////Make a thread to run the crawling animation and label.
            //Thread serviceThread = new Thread(new ThreadStart(CrawlingAnimation));
            //serviceThread.Name = "CrawlingAnimation";
            ////Make the CrawlingAnimation thread a background thread.
            //serviceThread.IsBackground = true;
            ////Start the Thread.
            //serviceThread.Start();

            


            graph.vertices.Clear();
            // Visuals happening pressing button 'Crawl'.
            pic_Mail.Visible = false;
            treeView.Visible = false;
            treeView.Nodes.Clear();
            lbl_LF.Visible = true;
            lbl_TotalLinksFound.Visible = true;
            richTextBox_Information.Text = "";
            pic_BrowserIcon.Visible = true;
            pic_ClearIcon.Visible = true;
            btn_Browser.Visible = true;
            pic_ArrowToClearCrawl.Visible = true;
            pic_Information.Visible = true;
            lbl_InfoHttp.Visible = true;
            lbl_Site.Visible = true;
            lbl_Site.Text = "Http://" + comboBox_Site.Text;
            lbl_Press.Visible = true;
            btn_Html.Visible = true;
            lbl_ToTreeView.Visible = true;
            richTextBox_Information.Visible = true;
            pic_ArrowToBtnHtml.Visible = true;
            btn_ClearAll.Visible = true;
            btn_ClearInformation.Visible = true;
            pic_ArrowToClearInfoBtn.Visible = true;
            pic_ArrowToOptions.Visible = true;
            btn_AbsoluteLinks.Visible = true;
            btn_SearchSelected.Visible = true;
            btn_Emails.Visible = true;
            lineShape.Visible = true;
            pic_CrawlDone.Visible = true;
            lbl_CrawlDone.Visible = true;
            pic_Connection.Visible = true;
            lbl_Connection.Visible = true;
            lineShape2.Visible = true;
            btn_TreeView.Visible = true;
            groupBox_Contains.Visible = true;
            lbl_TreeDepth.Visible = false;
            lbl_TreeOFDepth.Visible = false;
            lbl_TreeOf.Visible = false;
            lbl_TreeViewSite.Visible = false;
            pic_TreeView.Visible = false;
            pic_ArrowTree.Visible = false;
            pic_Contains.Visible = false;


            ////Make a thread to run the GetLinks.
            //Thread serviceThread = new Thread(new ThreadStart(GetLinks));
            //serviceThread.Name = "GetLinks";
            ////Make the GetLinks thread a background thread.
            //serviceThread.IsBackground = true;
            ////Start the Thread.
            //serviceThread.Start();
            GetLinks();



        }

        private void btn_ClearAll_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("This will clear all the crawling done!\nAre you sure you want to clear it?", "Warning", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                // Visuals happening pressing button 'Clear Crawl'.
                richTextBox_Information.Text = "";
                pic_BrowserIcon.Visible = false;
                pic_ClearIcon.Visible = false;
                btn_Browser.Visible = false;
                pic_ArrowToClearCrawl.Visible = false;
                pic_Information.Visible = false;
                lbl_InfoHttp.Visible = false;
                lbl_Site.Visible = false;
                lbl_Site.Text = "Http://" + comboBox_Site.Text;
                lbl_Press.Visible = false;
                btn_Html.Visible = false;
                lbl_ToTreeView.Visible = false;
                richTextBox_Information.Visible = false;
                pic_ArrowToBtnHtml.Visible = false;
                btn_ClearAll.Visible = false;
                comboBox_Site.Text = "";
                btn_ClearInformation.Visible = false;
                pic_ArrowToClearInfoBtn.Visible = false;
                pic_ArrowToOptions.Visible = false;
                btn_AbsoluteLinks.Visible = false;
                btn_SearchSelected.Visible = false;
                btn_Emails.Visible = false;
                lineShape.Visible = false;
                pic_CrawlDone.Visible = false;
                lbl_CrawlDone.Visible = false;
                pic_Connection.Visible = false;
                lbl_Connection.Visible = false;
                lineShape2.Visible = false;
                pic_ArrowAbsolute.Visible = false;
                pic_ArrowToContain.Visible = false;
                pic_ArrowEmails.Visible = false;
                btn_TreeView.Visible = false;
                lbl_LF.Visible = false;
                lbl_TotalLinksFound.Visible = false;
                groupBox_Contains.Visible = false;
                treeView.Visible = false;
                pic_ArrowTree.Visible = false;
               

                //
                lbl_SelectedTask.Visible = false;
                richTextBox_SelectedTask.Text = "";
                richTextBox_SelectedTask.Visible = false;
                pic_ArrowToSelectedTask.Visible = false;
                btn_ClearSelectedInfo.Visible = false;
                pic_ArrowToContain.Visible = false;
                pic_ArrowEmails.Visible = false;
                pic_ArrowAbsolute.Visible = false;
                lbl_Task.Visible = false;
                lbl_ClearTask.Visible = false;
                lbl_TreeDepth.Visible = false;
                lbl_TreeOFDepth.Visible = false;
                lbl_TreeOf.Visible = false;
                lbl_TreeViewSite.Visible = false;
                pic_TreeView.Visible = false;
                pic_ArrowTree.Visible = false;
                pic_Mail.Visible = false;
                //
                pic_NodeOptions.Visible = false;
                lbl_SelectNodeOption.Visible = false;
                btn_NodeHtml.Visible = false;
                btn_NodeBrowser.Visible = false;
            }
        }

        private void btn_ClearInformation_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("This will clear all the information text!\nAre you sure you want to clear it?", "Warning", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                richTextBox_Information.Text = "";
                pic_ArrowToClearInfoBtn.Visible = false;
                btn_ClearInformation.Visible = false;
                pic_ArrowAbsolute.Visible = false;
                pic_ArrowToContain.Visible = false;
                pic_ArrowEmails.Visible = false;
                lbl_LF.Visible = false;
                lbl_TotalLinksFound.Text = "";
                lbl_TotalLinksFound.Visible = false;
            }
        }

        // Get HTML for selected site.
        private void btn_Html_Click(object sender, EventArgs e)
        {
            HtmlView htmlView = new HtmlView();
            htmlView.Show();
            string startUrl = "Http://" + comboBox_Site.Text;
            string url = "";
            url = startUrl;



            // WebClient is a class which provided by C# which do all necessary work to establish a connect.
            WebClient webClient = new WebClient();

            // use a string to save all data from website.
            webClient.Encoding = System.Text.Encoding.UTF8;

            try
            {
                string strSource = webClient.DownloadString(url);
                htmlView.richTextBoxHtml.Text = strSource;
                htmlView.lbl_HtmlViewOf.Text = "HTML source of : " + url;
            }
            catch (Exception)
            {
                MessageBox.Show("Error : could not load html! please try again or another link.");
            }
            webClient.Dispose();


        }

        private void btn_Browser_Click(object sender, EventArgs e)
        {
            Browser browser = new Browser();
            try
            {
                browser.webBrowser.Navigate(new Uri("Http://" + comboBox_Site.Text));
                browser.Show();
            }
            catch (Exception)
            {
                MessageBox.Show("No site selected, select one from the dropdown or type in manually.");
            }

        }

        private void btn_AbsoluteLinks_Click(object sender, EventArgs e)
        {
            treeView.Visible = false;
            richTextBox_SelectedTask.Text = "";
            // Visuals.
            lbl_SelectedTask.Visible = true;
            lbl_Task.Visible = true;
            lbl_Task.Text = "Absolute Links";
            pic_ArrowAbsolute.Visible = true;
            btn_ClearInformation.Visible = true;
            pic_ArrowToClearInfoBtn.Visible = true;
            pic_ArrowToContain.Visible = false;
            pic_ArrowEmails.Visible = false;
            richTextBox_SelectedTask.Visible = true;
            pic_ArrowToSelectedTask.Visible = true;
            btn_ClearSelectedInfo.Visible = true;
            lbl_ClearTask.Visible = true;
            btn_ClearSelectedInfo.Text = "Absolute Links";
            pic_ArrowTree.Visible = false;
            lbl_TreeDepth.Visible = false;
            lbl_TreeOFDepth.Visible = false;
            pic_ArrowToContain.Visible = false;
            pic_Http.Visible = true;
            pic_Mail.Visible = false;
            pic_Contains.Visible = false;
            //
            pic_TreeView.Visible = false;
            lbl_TreeOf.Visible = false;
            lbl_TreeViewSite.Visible = false;
            //
            pic_NodeOptions.Visible = false;
            lbl_SelectNodeOption.Visible = false;
            btn_NodeHtml.Visible = false;
            btn_NodeBrowser.Visible = false;

            GetAbsoluteLinks();
        }

        private void btn_SearchSelected_Click(object sender, EventArgs e)
        {
            pic_Contains.Visible = true;
            treeView.Visible = false;
            richTextBox_SelectedTask.Text = "";
            // Visuals.
            lbl_SelectedTask.Visible = true;
            lbl_Task.Visible = true;
            lbl_Task.Text = "Search for : " + textBox_Contains.Text;
            pic_ArrowToContain.Visible = true;
            btn_ClearInformation.Visible = true;
            pic_ArrowToClearInfoBtn.Visible = true;
            pic_ArrowAbsolute.Visible = false;
            pic_ArrowEmails.Visible = false;
            richTextBox_SelectedTask.Visible = true;
            pic_ArrowToSelectedTask.Visible = true;
            btn_ClearSelectedInfo.Visible = true;
            btn_ClearSelectedInfo.Text = "Search";
            lbl_ClearTask.Visible = true;
            pic_ArrowTree.Visible = false;
            lbl_TreeDepth.Visible = false;
            lbl_TreeOFDepth.Visible = false;
            pic_Http.Visible = false;
            pic_Mail.Visible = false;

            lbl_TreeViewSite.Visible = false;
            lbl_TreeOf.Visible = false;
            pic_TreeView.Visible = false;
            //
            pic_NodeOptions.Visible = false;
            lbl_SelectNodeOption.Visible = false;
            btn_NodeHtml.Visible = false;
            btn_NodeBrowser.Visible = false;

            // Method get links that contains certain criteria.
            GetContains();
        }

        private void btn_Emails_Click(object sender, EventArgs e)
        {
            pic_Mail.Visible = true;
            treeView.Visible = false;
            richTextBox_SelectedTask.Text = "";
            // Visuals.
            lbl_SelectedTask.Visible = true;
            lbl_Task.Visible = true;
            lbl_Task.Text = "Emails";
            pic_ArrowEmails.Visible = true;
            btn_ClearInformation.Visible = true;
            pic_ArrowToClearInfoBtn.Visible = true;
            pic_ArrowAbsolute.Visible = false;
            pic_ArrowToContain.Visible = false;
            richTextBox_SelectedTask.Visible = true;
            pic_ArrowToSelectedTask.Visible = true;
            btn_ClearSelectedInfo.Visible = true;
            btn_ClearSelectedInfo.Text = "Emails";
            lbl_ClearTask.Visible = true;
            pic_ArrowTree.Visible = false;
            lbl_TreeDepth.Visible = false;
            lbl_TreeOFDepth.Visible = false;
            pic_ArrowToContain.Visible = false;
            pic_TreeView.Visible = false;
            lbl_TreeOf.Visible = false;
            lbl_TreeViewSite.Visible = false;
            pic_Http.Visible = false;
            pic_Contains.Visible = true;
            //
            pic_NodeOptions.Visible = false;
            lbl_SelectNodeOption.Visible = false;
            btn_NodeHtml.Visible = false;
            btn_NodeBrowser.Visible = false;
            
        }

        private void btn_ClearSelectedInfo_Click(object sender, EventArgs e)
        {
            lbl_SelectedTask.Visible = false;
            lbl_Task.Visible = false;
            lbl_Task.Text = "";
            richTextBox_SelectedTask.Text = "";
            pic_ArrowToSelectedTask.Visible = false;
            btn_ClearSelectedInfo.Visible = false;
            pic_ArrowToContain.Visible = false;
            pic_ArrowEmails.Visible = false;
            pic_ArrowAbsolute.Visible = false;
            richTextBox_SelectedTask.Visible = false;
            lbl_ClearTask.Visible = false;
            groupBox_Contains.Visible = false;
            lbl_TreeDepth.Visible = false;
            lbl_TreeOFDepth.Visible = false;
            pic_Http.Visible = false;
            pic_Mail.Visible = false;
            pic_Contains.Visible = true;


        }

        // Use HtmlAgilityPack to get all links from selected site and build the graph.
        public void GetLinks()
        {
            
                    string startUrl = comboBox_Site.Text;
                    Vertex v = new Vertex(comboBox_Site.Text);
                    graph.AddVertex(v);
                    getVertices(startUrl, graph, v);



                    for (int i = 1; graph.vertices.Count > i && i < Convert.ToInt32(comboBox_DepthSelect.Text); i++)// Depth search.
                    {

                        // Ser efter om linket er en hel hjemmeside eller blot et minor link.
                        if (graph.vertices[i].Name.Length > 4 && graph.vertices[i].Name.Substring(0, 4).Equals("http"))
                        {
                            // Ser efter om linket tilhører den indtastede hjemmeside 
                            if (graph.vertices[i].Name.Substring(0, startUrl.Length).Equals(startUrl))
                            {
                                getVertices(graph.vertices[i].Name, graph, graph.vertices[i]);
                            }
                        }



                        // Hvis ingen af de ovenstående er det et minor link og vi tilføjer det originale link
                        // og evt. "/" hvis nødvendigt. 
                        else
                        {

                            string innerUrl = startUrl;
                            if (graph.vertices[i].Name.Substring(0, 1).Equals("/"))
                            {
                                innerUrl += graph.vertices[i].Name;
                            }
                            else
                            {
                                innerUrl += "/" + graph.vertices[i].Name;
                            }
                            Console.WriteLine(innerUrl);
                            getVertices(innerUrl, graph, graph.vertices[i]);
                        }
                    }
                    // Display and graph using
                    richTextBox_Information.Text += "Done Crawling the Website and found : \n" + graph.vertices.Count + " vertices (Directly under Http://"+ startUrl +")\n\n";
                    lbl_TotalLinksFound.Text = Convert.ToString(graph.vertices.Count);



                    foreach (Vertex v1 in graph.vertices)
                    {
                        int nr = graph.vertices.IndexOf(v1);
                        nr++;

                        richTextBox_Information.Text += "Vertice nr.: " + nr + " Link: " + v1.Name + "\n";
                        richTextBox_Information.Text += "Vertices that the current vertex leads to: " + " \n";

                        foreach (Edge e in v1.Edges)
                        {

                            if (!e.To.Name.Equals(v1.Name))
                            {
                                richTextBox_Information.Text += e.To.Name + "\n";


                            }
                            if (e.From.Name.Equals(e.To.Name))
                            {
                                richTextBox_Information.Text += v1.Name + " leads to itself. \n";

                            }
                        }
                        richTextBox_Information.Text += "\n";
                        
                    }
                    constructTree();
                
            }
        
        public List<Vertex> getVertices(string url, BasicGraph graph, Vertex parentVertex)
        {
            HtmlWeb htmlWeb = new HtmlWeb();
            HtmlAgilityPack.HtmlDocument doc = htmlWeb.Load("Http://" + comboBox_Site.Text);

            if (doc.DocumentNode.SelectNodes("//a[@href]") == null)
            {
                Console.WriteLine(url + "Is empty");
            }
            else
            {
                foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
                {

                    HtmlAgilityPack.HtmlAttribute att = link.Attributes["href"];
                    if (!att.Value.Equals("") && !att.Value.Contains("javascript"))
                    {
                        bool visited = false;
                        foreach (Vertex v in graph.vertices)
                        {
                            if (att.Value.Equals(v.Name))
                            {
                                Edge e = new Edge(parentVertex, v, 1);
                                v.AddEdge(e);
                                parentVertex.AddEdge(e);
                                visited = true;
                            }
                        }
                        if (visited == false)
                        {
                            // create vertex and add edge on both the new vertex and parent vertex
                            Vertex v = new Vertex(att.Value);
                            Edge e = new Edge(parentVertex, v, 1);
                            v.AddEdge(e);
                            parentVertex.AddEdge(e);
                            graph.AddVertex(v);
                        }
                    }

                    // Nedenunder ser den om alle nodes er taget, oven over får vi selve URL til hver link

                }
            }
            return graph.vertices;
        }

        private List<TreeNode> treeNodes;
        public void constructTree()
        {
            treeNodes = new List<TreeNode>();
            int i = 0;
            foreach (Vertex v2 in graph.vertices)
            {
                TreeNode newTree = new TreeNode(v2.Name);
                newTree.Text = v2.Name;
                newTree.Name = v2.Name;
                treeNodes.Add(newTree);
                // Makes sure it only adds the root tree.
                if (i == 0)
                {
                    treeView.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
                                        newTree});
                    i++;
                }
                // Looking for treeNodes this might be attached to. 
            }
            combineEdges(treeNodes);

        }
        public void combineEdges(List<TreeNode> treeNodes)
        {
            foreach (Vertex v2 in graph.vertices)
            {
                TreeNode newTree = null;
                foreach (TreeNode tree in treeNodes)
                {
                    if (tree.Name.Equals(v2.Name))
                    {
                        newTree = tree;
                    }
                }
                foreach (Edge e in v2.Edges)
                {
                    if (!e.From.Name.Equals(v2.Name))
                    {
                        for (int index = 0; index < treeNodes.Count; index++)
                        //    foreach (TreeNode t in treeNodes)
                        {
                            if (treeNodes[index].Name.Equals(e.From.Name))
                            {
                                if (newTree.Parent == null)
                                {
                                    treeNodes[index].Nodes.Add(newTree);
                                }
                                else
                                {
                                    TreeNode copyTree = new TreeNode(v2.Name);
                                    copyTree.Text = v2.Name;
                                    copyTree.Name = v2.Name;
                                    treeNodes.Add(copyTree);
                                    treeNodes[index].Nodes.Add(copyTree);
                                }
                            }
                        }
                    }
                }
            }
            

        }


        private void btn_TreeView_Click(object sender, EventArgs e)
        {
            pic_NodeOptions.Visible = true;
            lbl_SelectNodeOption.Visible = true;
            btn_NodeHtml.Visible = true;
            btn_NodeBrowser.Visible = true;
            pic_ArrowTree.Visible = true;
            treeView.Visible = true;
            lbl_SelectedTask.Visible = false;
            lbl_Task.Visible = false;
            pic_ArrowToSelectedTask.Visible = false;
            lbl_ClearTask.Visible = false;
            btn_ClearSelectedInfo.Visible = false;
            pic_ArrowEmails.Visible = false;
            pic_ArrowAbsolute.Visible = false;
            pic_TreeView.Visible = true;
            lbl_TreeOf.Visible = true;
            lbl_TreeViewSite.Text = "Http://" + comboBox_Site.Text;
            lbl_TreeViewSite.Visible = true;
            lbl_TreeDepth.Text = comboBox_DepthSelect.Text;
            lbl_TreeDepth.Visible = true;
            lbl_TreeOFDepth.Visible = true;
            pic_ArrowToContain.Visible = false;
            pic_Http.Visible = false;
            pic_Contains.Visible = false;
            pic_Mail.Visible = false;
        }



        //Get Absolute Links.
        public void GetAbsoluteLinks()
        {
            try
            {
                // Using HtmlAgilityPack to load HTML from URL.
                HtmlWeb htmlWeb = new HtmlWeb();

                // Creates an HtmlDocument object from an URL
                HtmlAgilityPack.HtmlDocument document = htmlWeb.Load("Http://" + comboBox_Site.Text);

                List<string> hrefTags = new List<string>();

                if (hrefTags != null)
                {
                    foreach (HtmlAgilityPack.HtmlNode node in document.DocumentNode.SelectNodes("//a[@href]"))
                    {
                        //HtmlAgilityPack.HtmlAttribute att = node.Attributes["href"];
                        //if (!att.Value.Equals("") && !att.Value.Contains("javascript"))
                        {
                            // Add each link to the list 'hrefTags'.
                            hrefTags.Add(node.Attributes["href"].Value);
                        }

                    }
                }
                else
                {
                    MessageBox.Show("No Links Found");
                }
                if (hrefTags != null)
                {

                    // View Absolute Links.
                    foreach (var link in from l in hrefTags where l.StartsWith("http://") select l)
                    {
                        richTextBox_SelectedTask.Text += "Absolute link: " + "\n" + link + "\n";
                    }

                }
                else
                {
                    richTextBox_SelectedTask.Text = "No Absolute Link Values Found";
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Please type in your Http:// search site in the textbox or select one from dropdown");
            }
        }

        // Get ContainsLinks.
        public void GetContains()
        {
            try
            {
                // Using HtmlAgilityPack to load HTML from URL.
                HtmlWeb htmlWeb = new HtmlWeb();

                // Creates an HtmlDocument object from an URL
                HtmlAgilityPack.HtmlDocument document = htmlWeb.Load("Http://" + comboBox_Site.Text);

                List<string> hrefTags = new List<string>();

                if (hrefTags != null)
                {
                    foreach (HtmlAgilityPack.HtmlNode node in document.DocumentNode.SelectNodes("//a[@href]"))
                    {
                        //HtmlAgilityPack.HtmlAttribute att = node.Attributes["href"];
                        //if (!att.Value.Equals("") && !att.Value.Contains("javascript"))
                        {
                            // Add each link to the list 'hrefTags'.
                            hrefTags.Add(node.Attributes["href"].Value);
                        }

                    }
                }
                else
                {
                    MessageBox.Show("No Links Found");
                }
                if (hrefTags != null)
                {

                    // View Absolute Links.
                    foreach (var link in from l in hrefTags where l.Contains(textBox_Contains.Text) select l)
                    {
                        richTextBox_SelectedTask.Text += "Contains the characters [ " + textBox_Contains.Text + " ]" + "\n" + link + "\n";
                    }



                }
                else
                {
                    richTextBox_SelectedTask.Text = "No Images Found";
                }

            }

            // If no site selected.
            catch (Exception)
            {
                MessageBox.Show("Please type in your Http:// search site in the textbox or select one from dropdown");
            }


        }

        private void btn_NodeBrowser_Click(object sender, EventArgs e)
        {
            NodeBrowser nodeBrowser = new NodeBrowser();
            string startUrl = comboBox_Site.Text;
            string url = "";
            if (treeView.SelectedNode != null)
            {
                string path = treeView.SelectedNode.Name;

                if (path.Length > 4 && path.Substring(0, 4).Equals("http"))
                {
                    // Ser efter om linket tilhører den indtastede hjemmeside 
                    url = path;

                }
                // Hvis ingen af de ovenstående er det et minor link og vi tilføjer det originale link
                // og evt. "/" hvis nødvendigt. 
                else
                {
                    url = "Http://" + path;
                }
            }
            else
            {
                url = startUrl;
            }
            try
            {
                // Selected Http from node.
                nodeBrowser.nodeWebBrowser.Navigate(new Uri(url));
                nodeBrowser.Show();
            }
            catch (Exception)
            {
                MessageBox.Show("No site selected, select one from the treeView containing Http://");
            }
        }

        private void btn_NodeHtml_Click(object sender, EventArgs e)
        {
            NodeHtml nodeHtml = new NodeHtml();
            nodeHtml.Show();
            string startUrl = comboBox_Site.Text;
            string url = "";

            //
            
            
            if (treeView.SelectedNode != null)
            {
                string path = treeView.SelectedNode.Name;

                if (path.Length > 4 && path.Substring(0, 4).Equals("http"))
                {
                    // Ser efter om linket tilhører den indtastede hjemmeside 
                    url = path;

                }
                // Hvis ingen af de ovenstående er det et minor link og vi tilføjer det originale link
                // og evt. "/" hvis nødvendigt. 
                else
                {
                    url = startUrl + path;
                }
            }
            else
            {
                url = startUrl;
            }

            // WebClient is a class which provided by C# which do all necessary work to establish a connect.
            WebClient webClient = new WebClient();

            // use a string to save all data from website.
            webClient.Encoding = System.Text.Encoding.UTF8;

            try
            {
                string strSource = webClient.DownloadString(url);
                nodeHtml.richTextBox_NodeHtml.Text = strSource;
                nodeHtml.lbl_NodeUrl.Text = "HTML source of : " + url;
            }
            catch (Exception)
            {
                MessageBox.Show("Error : could not load html! please try again or another node.");
            }
            webClient.Dispose();
        }

        
    }
 

}














